package com.etc.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.etc.entity.Prices;
import com.etc.entity.Users;
import com.etc.entity.jsonui.JsonUi;
import com.etc.service.PricesService;
import com.etc.service.impl.PricesServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class AdminsPriceServlet
 */
@WebServlet("/aps.do")
public class AdminsPriceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PricesService pricesService = new PricesServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AdminsPriceServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		// response.setContentType("application/json");
		// 创建PrintWriter 方便后面再页面上输出数据

		// 执行封装的分页查询
		String op = "query";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if (op.equals("query")) {
			PageDoQuery(request, response);
		}
		// 执行删除方法
		else if (op.equals("del")) {
			int id = Integer.valueOf(request.getParameter("id"));
			boolean flag = pricesService.DelPrices(id);
			// 获取到Id后打印
			PrintWriter out = response.getWriter();
			out.print(flag);
			out.close();
		} else if (op.equals("update")) {
			// 这时候用的是request.getReader();这个方法是用于获取页面一侧的ajax请求中的Json数据
			BufferedReader reader = request.getReader();
			// 用字符串拼接的方式来拼接接收到的JSON数据
			StringBuffer sBuffer = new StringBuffer();
			// 定义一个字符串让他为空，在下面的while循环中不断赋予它值，使得字符串可以拼接
			String str = null;
			while ((str = reader.readLine()) != null) {
				sBuffer.append(str);
			}
			System.out.println(sBuffer);
			// 将GSON数据转化成Users对象，方便后面传入数据库
			Gson gson = new Gson();
			// 转换对象的方式，并赋予users
			Prices prices = gson.fromJson(sBuffer + "", Prices.class);
			System.out.println(prices);

			// int id =Integer.parseInt(request.getParameter("id"));

			boolean flag = pricesService.UpdPrices(prices);
			PrintWriter out = response.getWriter();
			out.print(flag);
			out.close();
			System.out.println(flag);
		}else if(op.equals("add")) {
			float waterCount = Float.parseFloat(request.getParameter("waterCount"));
			float waterPay = Float.parseFloat(request.getParameter("waterPay"));
			float eleCount = Float.parseFloat(request.getParameter("eleCount"));
			float elePay = Float.parseFloat(request.getParameter("elePay"));
			float housePay = Float.parseFloat(request.getParameter("housePay"));
			String houseName = request.getParameter("houseName");
			String houseDoorNo = request.getParameter("houseDoorNo");
			int houseState = Integer.parseInt(request.getParameter("houseState"));
			String priceRemark = request.getParameter("priceRemark");
			Prices prices = new Prices(waterCount, waterPay, eleCount, elePay, housePay, houseName, houseDoorNo, houseState, priceRemark);
			boolean flag = pricesService.AddPrices(prices);
			request.getRequestDispatcher("admin_prices.jsp").forward(request, response);
			PrintWriter out = response.getWriter();
			out.print(flag);
			out.close();
		}
		else if(op.equals("removesession")) {
			//退出后清除所有的session信息
			request.getSession().invalidate();
			request.getRequestDispatcher("index.jsp").forward(request, response);

		}
	}

	private void PageDoQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		int page = 1;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		int limit = 10;
		if (request.getParameter("limit") != null) {
			limit = Integer.parseInt(request.getParameter("limit"));
		}

		String strLike = "";
		if (request.getParameter("strLike") != null) {
			strLike = request.getParameter("strLike");
		}
		PageData<Prices> pd = pricesService.GetPricesAll(page, limit, strLike);

		JsonUi<Prices> jsonUi = new JsonUi<>(0, "", pd.getTotalCount(), pd.getData());

		Gson gson = new Gson();
		String string = gson.toJson(jsonUi);

		out.print(string);

		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
